import { computed } from 'vue'
import { useStore } from 'vuex'
export const useCommonCartEffect = shopId => {
  const store = useStore()
  const cartList = store.state.cartList
  const ChangeCartItemInfo = (shopId, productId, productInfo, num) => {
    store.commit('ChangeCartItemInfo', { shopId, productId, productInfo, num })
    // console.log(shopId, productId, productInfo)
  }
  const productList = computed(() => {
    const productList = store.state.cartList[shopId]?.productList || {}
    return productList
  })
  const shopName = computed(() => {
    const shopName = cartList[shopId]?.shopName || ''
    return shopName
  })
  const price = computed(() => {
    const productList = cartList[shopId]?.productList
    let totalPrice = 0
    if (productList) {
      for (const key in productList) {
        const product = productList[key]
        if (product.check) {
          totalPrice += product.count * product.price
        }
      }
    }
    return totalPrice.toFixed(2)
  })

  return { cartList, shopName, productList, ChangeCartItemInfo, price }
}
